package com.example.service.impl;

import com.example.dao.GoodsMapper;
import com.example.model.Goods;
import com.example.service.GoodsService;
import com.example.thread.HbaseThread;
import com.example.util.AssertUtil;
import com.example.util.GoodsThreadPool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Random;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {

    GoodsMapper goodsMapper;

    @Resource
    public void setGoodsMapper(GoodsMapper goodsMapper) {
        this.goodsMapper = goodsMapper;
    }


    @Override
    public Goods getGoodsById(Long goodsId) {
        return goodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public void updateGoodsById(Integer number, Long goodIs) {
        goodsMapper.updateNumByGoodsId(number, goodIs);
    }

    @Override
    public void addGoods(Goods goods) {
        if (AssertUtil.isEmpty(goods)) {
            //todo  throw Exception
        }
        Random random = new Random();
        goods.setGoodsId(Long.valueOf(random.nextInt(5000)));
        goodsMapper.insert(goods);
        // todo insert into hbase
        GoodsThreadPool.runThread(new HbaseThread());


    }
}
